/*
 *  (C) Copyright 2008 Insula Tecnologia da Informacao Ltda.
 * 
 *  This file is part of Vulcano.
 *
 *  Vulcano is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Vulcano is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with Vulcano.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.openinsula.vulcano.orm.command.transaction;

import java.util.Collection;
import java.util.List;

import org.openinsula.vulcano.core.command.Command;
import org.openinsula.vulcano.core.command.transaction.TransactionInvoker;
import org.openinsula.vulcano.orm.command.DaoCommandFactory;
import org.openinsula.vulcano.orm.command.transaction.operations.DaoCommandTransactionOperations;
import org.openinsula.vulcano.orm.command.transaction.operations.DaoCommandTransactionOperationsDelegate;
import org.openinsula.vulcano.orm.dao.query.DaoQuery;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.PlatformTransactionManager;

@Component("transactionFacade")
public class DaoCommandTransactionFacadeImpl implements DaoCommandTransactionOperations, DaoCommandTransactionFacade {

	@Autowired
	protected TransactionInvoker transactionInvoker;

	@Autowired
	protected DaoCommandFactory daoCommandFactory;
	
	protected PlatformTransactionManager transactionManager;
	
	private DaoCommandTransactionOperations operationsDelegate;

	public void setTransactionManager(final PlatformTransactionManager transactionManager) {
		this.transactionManager = transactionManager;
	}
	
	private DaoCommandTransactionOperations getOperationsDelegate() {
		if (operationsDelegate == null) {
			operationsDelegate = new DaoCommandTransactionOperationsDelegate(transactionManager, transactionInvoker, daoCommandFactory);
		}
		
		return operationsDelegate;
	}
	
	@Override
	public <T> int delete(final T... entities) {
		return getOperationsDelegate().delete(entities);
	}

	@Override
	public int deleteAll(final Collection<?> entities) {
		return getOperationsDelegate().deleteAll(entities);
	}

	@Override
	public int deleteAll(final Class<?> entityClass) {
		return getOperationsDelegate().deleteAll(entityClass);
	}

	@Override
	public <T> List<T> find(final String queryString, final Object... values) {
		return getOperationsDelegate().<T>find(queryString, values);
	}
	
	@Override
	public <T> List<T> find(final InitializeCallback<List<T>> initializeCallback, final String queryString, final Object... values) {
		return getOperationsDelegate().<T>find(initializeCallback, queryString, values);
	}

	@Override
	public <T> T findById(final Class<T> entityClass, final Object id) {
		return getOperationsDelegate().findById(entityClass, id);
	}
	
	@Override
	public <T> T findById(final InitializeCallback<T> initializeCallback, final Class<T> entityClass, final Object id) {
		return getOperationsDelegate().findById(initializeCallback, entityClass, id);
	}

	@Override
	public <T> List<T> listAll(final Class<T> entityClass) {
		return getOperationsDelegate().listAll(entityClass);
	}
	
	@Override
	public <T> List<T> listAll(final InitializeCallback<List<T>> initializeCallback, final Class<T> entityClass) {
		return getOperationsDelegate().listAll(initializeCallback, entityClass);
	}

	@Override
	public void reattach(final Object entity) {
		getOperationsDelegate().reattach(entity);
	}
	
	@Override
	public <T> void reattach(final InitializeCallback<T> initializeCallback, final T entity) {
		getOperationsDelegate().reattach(initializeCallback, entity);
	}

	@Override
	public void refresh(final Object entity) {
		getOperationsDelegate().refresh(entity);
	}
	
	@Override
	public <T> void refresh(final InitializeCallback<T> initializeCallback, final T entity) {
		getOperationsDelegate().refresh(initializeCallback, entity);
	}

	@Override
	public <T> int save(final T... entities) {
		return getOperationsDelegate().save(entities);
	}

	@Override
	public int saveAll(final Collection<?> entities) {
		return getOperationsDelegate().saveAll(entities);
	}
	
	@Override
	public <T> T merge(final T entity) {
		return getOperationsDelegate().merge(entity);
	}

	@Override
	public int update(final String queryString, final Object... values) {
		return getOperationsDelegate().update(queryString, values);
	}

	@Override
	public <T> T invoke(final Command<T> command) {
		return transactionInvoker.invoke(command, transactionManager);
	}

	@Override
	public <T> List<T> find(final DaoQuery daoQuery) {
		return getOperationsDelegate().<T>find(daoQuery);
	}
	
	@Override
	public <T> List<T> find(final InitializeCallback<List<T>> initializeCallback, final DaoQuery daoQuery) {
		return getOperationsDelegate().<T>find(initializeCallback, daoQuery);
	}

	@Override
	public <T> T findUnique(final DaoQuery daoQuery) {
		return getOperationsDelegate().<T>findUnique(daoQuery);
	}
	
	@Override
	public <T> T findUnique(final InitializeCallback<T> initializeCallback, final DaoQuery daoQuery) {
		return getOperationsDelegate().<T>findUnique(initializeCallback, daoQuery);
	}
	
	@Override
	public <T> T findUnique(final String queryString, final Object... values) {
		return getOperationsDelegate().<T>findUnique(queryString, values);
	}
	
	@Override
	public <T> T findUnique(final InitializeCallback<T> initializeCallback, final String queryString, final Object... values) {
		return getOperationsDelegate().<T>findUnique(initializeCallback, queryString, values);
	}
	
	@Override
	public TransactionInvoker getTransactionInvoker() {
		return transactionInvoker;
	}
	
	@Override
	public PlatformTransactionManager getTransactionManager() {
		return transactionManager == null ? transactionInvoker.getDefaultTransactionManager() : transactionManager;
	}

}
